package com.jsnkj.basic.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.jsnkj.basic.R
import com.jsnkj.basic.activity.DownActivity
import com.jsnkj.basic.bean.HomeItemBean
import com.jsnkj.basic.bean.InvitesBean
import com.jsnkj.basic.util.jsonToString
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions


/**
 * 分享 邀请弹窗
 */
class VersionUpdateDialog(
    var dialogContext: Activity,
    var appVersion: String,
    var inviteBean: InvitesBean
) : Dialog(dialogContext), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_version_update)
        setCanceledOnTouchOutside(false)
        setCancelable(false)
        window?.setBackgroundDrawableResource(android.R.color.transparent)
        initView()
    }

    fun initView() {

        if (appVersion.isNullOrEmpty()){

        }else{
            findViewById<TextView>(R.id.tv_new_version).text = appVersion
        }



        findViewById<TextView>(R.id.tv_confirm).setOnClickListener(this)
        findViewById<TextView>(R.id.tv_web).setOnClickListener(this)
        findViewById<ImageView>(R.id.img_close).setOnClickListener(this)
    }


    override fun onClick(view: View) {
        when (view.id) {
            R.id.img_close -> {
//                dismiss()
//                click.closeItemClick()
            }

            R.id.tv_confirm -> {
                val bean = HomeItemBean()

                bean.id = 0
                bean.icon = ""
                bean.title = dialogContext.getString(R.string.app_name)
                bean.remark = "让赚钱变得如此简单"
                bean.down_url = inviteBean.getDownUrl()

                XXPermissions.with(dialogContext) // 申请单个权限
                    .permission(Permission.Group.STORAGE)// 申请多个权限
                    .request(object : OnPermissionCallback {
                        override fun onGranted(permissions: List<String>, all: Boolean) {
                            if (all) {
                                DownActivity.startDownActivity(
                                    dialogContext,
                                    bean.jsonToString(),
                                    true
                                )
                            } else {
                                com.jsnkj.basic.util.ToastUtils.show("获取部分权限成功，但部分权限未正常授予")
                            }
                        }

                        override fun onDenied(permissions: List<String>, never: Boolean) {
                            if (never) {
                                com.jsnkj.basic.util.ToastUtils.show("被永久拒绝授权，请手动授予存储权限")
                                // 如果是被永久拒绝就跳转到应用权限系统设置页面
                                XXPermissions.startPermissionActivity(dialogContext, permissions)
                            } else {
                                com.jsnkj.basic.util.ToastUtils.show("获取存储权限失败")
                            }
                        }
                    })


            }

            R.id.tv_web -> {
                if (inviteBean.getDownUrl().isNullOrEmpty()) {
                    com.jsnkj.basic.util.ToastUtils.show("apk下载地址错误")
                    return
                }
                try {
                    val uri = Uri.parse(inviteBean.getDownUrl())
                    val intent = Intent()
                    intent.setAction(Intent.ACTION_VIEW)
                    intent.setData(uri)
                    //是否安装浏览器
                    if (intent.resolveActivity(dialogContext.packageManager) != null) {
                        val componentName = intent.resolveActivity(dialogContext.packageManager)
                        com.jsnkj.basic.util.LogUtils.e("onConfigurationChanged: $componentName")
                        com.jsnkj.basic.util.LogUtils.e("onConfigurationChanged: " + componentName.className)
                        com.jsnkj.basic.util.LogUtils.e("onConfigurationChanged: " + componentName.packageName)
                        dialogContext.startActivity(intent)
                    } else {
                        com.jsnkj.basic.util.ToastUtils.show("请下载浏览器")
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    com.jsnkj.basic.util.LogUtils.e("跳转失败： ${e.toString()}")
                    com.jsnkj.basic.util.ToastUtils.show(e.message)
                }
            }
        }
    }

    interface onItemClickListener {
        fun closeItemClick()

    }
}